package org.lt.cj.view.swing;

import org.lt.cj.config.ServerProperty;
import org.lt.cj.model.timer.TranslateTimer;

/**
 *
 * @author zgh
 */
public class TranslateDialog extends javax.swing.JDialog {

    /** Creates new form TranslateDialog */
    public TranslateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initData();
    }

    private void initData() {
        ServerProperty sp = new ServerProperty();
        jOrigTextField.setText(sp.getValue("languageFrom"));
        jTargetTextField.setText(sp.getValue("languageTo"));
        jCategoryTextField.setText(sp.getValue("cateCount"));
        jProductTextField.setText(sp.getValue("productCount"));
        jAttributeTextField.setText(sp.getValue("attributeCount"));
        jEvaluationTextField.setText(sp.getValue("evaluationCount"));
        jDescriptionTextField.setText(sp.getValue("descriptionCount"));
        sp.closeI();

//        sellerCount = (sp.getValue("sellerCount") != null && !sp.getValue(
//                "sellerCount").equals("")) ? Integer.parseInt(sp.getValue("sellerCount")) : 100;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCancelButton = new javax.swing.JButton();
        jTranslateToggleButton = new javax.swing.JToggleButton();
        jOrigLabel = new javax.swing.JLabel();
        jTargetLabel = new javax.swing.JLabel();
        jOrigTextField = new javax.swing.JTextField();
        jTargetTextField = new javax.swing.JTextField();
        jSetLabel = new javax.swing.JLabel();
        jSaveButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jCategoryTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jProductTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jAttributeTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jEvaluationTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jDescriptionTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("翻译对话框");

        jCancelButton.setText("取消");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        jTranslateToggleButton.setText("开启");
        jTranslateToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTranslateToggleButtonActionPerformed(evt);
            }
        });

        jOrigLabel.setText("源语言：");

        jTargetLabel.setText("目标语言：");

        jSetLabel.setText("语言设置(中文：zh，英语：en)");

        jSaveButton.setText("保存设置");
        jSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSaveButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("分类：");

        jLabel2.setText("翻译数量设置(条/次)");

        jLabel3.setText("商品：");

        jLabel4.setText("属性：");

        jLabel5.setText("评价：");

        jLabel6.setText("详情：");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSetLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(78, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jCategoryTextField)
                                    .addComponent(jProductTextField)
                                    .addComponent(jDescriptionTextField)
                                    .addComponent(jAttributeTextField, 0, 0, Short.MAX_VALUE)
                                    .addComponent(jEvaluationTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jOrigLabel)
                                    .addComponent(jTargetLabel))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTargetTextField)
                                    .addComponent(jOrigTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)))
                            .addComponent(jSaveButton))
                        .addGap(63, 63, 63))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTranslateToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9)))
                .addGap(41, 41, 41))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jSetLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jOrigLabel)
                    .addComponent(jOrigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTargetLabel)
                    .addComponent(jTargetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jProductTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAttributeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jEvaluationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jSaveButton)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTranslateToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCancelButton))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // 退出
        this.dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

    private void jTranslateToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTranslateToggleButtonActionPerformed
        // 开关
        if (jTranslateToggleButton.getText().equals("开启")) {
            System.out.println("------------翻译开始-----------");
            jTranslateToggleButton.setText("关闭");
            tt.executeTransferTimer(); //启动翻译线程
        } else {
            System.out.println("------------翻译停止-----------");
            jTranslateToggleButton.setText("开启");
            tt.stop();
        }
    }//GEN-LAST:event_jTranslateToggleButtonActionPerformed

    private void jSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaveButtonActionPerformed
        // 保存设置
        saveData();
    }//GEN-LAST:event_jSaveButtonActionPerformed

    public void saveData() {
        ServerProperty sp = new ServerProperty();
        sp.load();
        sp.setProperty("languageFrom", jOrigTextField.getText());
        sp.setProperty("languageTo", jTargetTextField.getText());
        sp.setProperty("cateCount", jCategoryTextField.getText());
        sp.setProperty("productCount", jProductTextField.getText());
        sp.setProperty("attributeCount", jAttributeTextField.getText());
        sp.setProperty("evaluationCount", jEvaluationTextField.getText());
        sp.setProperty("descriptionCount", jDescriptionTextField.getText());
        sp.store("save");
        sp.closeIO();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TranslateDialog dialog = new TranslateDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAttributeTextField;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JTextField jCategoryTextField;
    private javax.swing.JTextField jDescriptionTextField;
    private javax.swing.JTextField jEvaluationTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jOrigLabel;
    private javax.swing.JTextField jOrigTextField;
    private javax.swing.JTextField jProductTextField;
    private javax.swing.JButton jSaveButton;
    private javax.swing.JLabel jSetLabel;
    private javax.swing.JLabel jTargetLabel;
    private javax.swing.JTextField jTargetTextField;
    private javax.swing.JToggleButton jTranslateToggleButton;
    // End of variables declaration//GEN-END:variables
    private TranslateTimer tt = new TranslateTimer();
}
